package cn.meetingroom.service;


import cn.meetingroom.dao.UserDao;
import cn.meetingroom.model.User;

import java.util.List;

public class UserService {
    private UserDao userDao = new UserDao();
    //检查用户表中是否存在此项
    public boolean isExist(String colName, String value){
        if (userDao.find(colName,value) != null){
            return true;
        } else {
            return false;
        }
    }

    //添加新用户
    public boolean addUser(User user){
        if (userDao.addUser(user)){
            return true;
        } else {
            return false;
        }
    }

    public boolean delUser(long id){
        if (userDao.delUser(id)) return true;
        return false;
    }

    public boolean alterUser(User user){
        if (userDao.alterUser(user)) return true;
        return false;
    }

    public List<User> getUserList(){
        return userDao.getUserList();
    }

    //用户名密码校验
    public User checkUser(int role,String value,String password){
        if (role == 1) {
            User admin = userDao.find("username",value);
            if (admin != null){
                if (admin.getRole() == 1 && password.equals(admin.getPassword())) return admin;
                return null;
            }
            return null;
        } else {
            User userP = userDao.find("phone",value);
            User userE = userDao.find("email",value);
            if ( userP != null){
                if (password.equals(userP.getPassword())) return userP;
                return null;
            } else if (userE != null) {
                if (password.equals(userE.getPassword())) return userE;
                return null;
            }
            return null;
        }
    }

    public User getUserById(long id){
        return userDao.find("id",id);
    }
}
